﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lib.CommonForm;

namespace AUI
{
    public partial class frmEmployeeInfo : FormBase
    {
        int _EmployeeId = 0;
        DataRow m_dr;
        public frmEmployeeInfo(DataRow dr)
        {
            InitializeComponent();
            

            this.txtEmployeeCode.MaxLength = tbl_Employees_Length.EmployeeCode;
            this.txtEmployeeName.MaxLength = tbl_Employees_Length.EmployeeName;
            this.txtAddress.MaxLength = tbl_Employees_Length.Address;
            this.txtEmail.MaxLength = tbl_Employees_Length.Email;
            this.txtPhone.MaxLength = tbl_Employees_Length.Phone;
            this.txtMobile.MaxLength = tbl_Employees_Length.Mobile;
            this.txtPosition.MaxLength = tbl_Employees_Length.Position;

            _listRole = tbl_RoleRight_Controller.Instance().GetInfo("NhanVien");
            FormGlobals.Control_SetRequired(txtEmployeeCode, txtEmployeeName, cboBranchId);
            LoadComboBox.BranchID_Input(cboBranchId);
            LoadComboBox.DepartmentID_Input(cboDepartmentId, Globals.ToInt32(cboBranchId.SelectedValue));
            cboBranchId.SelectedIndexChanged += new EventHandler(cboBranch_SelectedIndexChanged);
            m_dr = dr;
            if (m_dr != null)
            {
                FormGlobals.Panel_FillData(this, dr);
                _EmployeeId = Globals.ToInt32(dr["EmployeeId"]);
            }
            
            
            _IsLoading = false;
        }

        public override void Show_Form()
        {
            this.ShowDialog();
        }

        void cboBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!_IsLoading)
            {
                LoadComboBox.DepartmentID_Input(cboDepartmentId, FormGlobals.ToInt32(cboBranchId.SelectedValue));
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            tbl_Employees_Info i = new tbl_Employees_Info();
            FormGlobals.Panel_GetData(this, i);
            i.EmployeeId = _EmployeeId;
            i.CreateUser = FormGlobals.strUserName;
            i.LastUser = FormGlobals.strUserName;
            _EmployeeId = tbl_Employees_Controller.Instance().Save(i);
            if (_EmployeeId > 0)
            {
                FormGlobals.Message_InTaskbar(UIMessage.SAVE_COMPLETE);
                _dlgResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
